package com.jvk.check4home;

import java.util.ArrayList;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jvk.check4home.frwk.homeDTO;

public class HomeAdapter implements ListAdapter {

	private ArrayList<homeDTO> _myListDto;
	private Context context;
	
	public interface OnSelectHome
	{
		public void selectDto(homeDTO p_home);
		
	}
	private OnSelectHome onSelectDto;
	
	public HomeAdapter(HomeList homeListener,ArrayList<homeDTO> myListDto) {
		super();
		_myListDto = myListDto;
		context = homeListener.getApplicationContext();
		onSelectDto=homeListener;
	}	
	
	public int getCount() {
		return _myListDto.size();
	}

	public Object getItem(int arg0) {
		return _myListDto.get(arg0);
	}

	public long getItemId(int arg0) {
		return arg0;
	}

	public int getItemViewType(int arg0) {
		return 1;
	}

	public View getView(int arg0, View arg1, ViewGroup arg2) {
		RelativeLayout rowlayout;
		homeDTO sr=_myListDto.get(arg0);
		if (arg1==null)
		{
			rowlayout = generateRowView(arg2, sr);
			
		}
		else
		{
			rowlayout=(RelativeLayout) arg1;
			
		}
		return rowlayout;
	}
	/*
	private void updateState(BuildingDto dto,String state)
	{
		int posi=_myListDto.indexOf(dto);
		
		
	}
	*/
	private RelativeLayout generateRowView(ViewGroup parent, homeDTO sr) {
	//	int posi=_myListDto.indexOf(sr);
		RelativeLayout rowlayout;
		rowlayout=(RelativeLayout) LayoutInflater.from(context).inflate(R.layout.celldata, parent,false);
		TextView title= (TextView) rowlayout.findViewById(R.id.cell_txt_name);
		TextView metros= (TextView) rowlayout.findViewById(R.id.cell_txt_scuarem);
		TextView habitac= (TextView) rowlayout.findViewById(R.id.cell_text_habit);
		TextView banios= (TextView) rowlayout.findViewById(R.id.cell_text_ban);
		TextView precio= (TextView) rowlayout.findViewById(R.id.cell_text_precio);
		//TextView rating= (TextView) rowlayout.findViewById(R.id.);
		title.setText(sr.name);
		metros.setText(sr.scuareM+" m² ");
		habitac.setText(sr.rooms+" "+context.getString(R.string.room));
		banios.setText(sr.bathrooms+" "+context.getString(R.string.bath));
		precio.setText(sr.price+" k");
		rowlayout.setTag(sr);
		RelativeLayout lo=(RelativeLayout) rowlayout.findViewById(R.id.cell_layout);
		if ( sr.pos%2==1)
		{
			
			lo.setBackgroundColor(0xfff7fdff);
		}else
		{
			lo.setBackgroundColor(0xffdff3ff);
		}
		rowlayout.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				homeDTO sr=(homeDTO) v.getTag();
				onSelectDto.selectDto(sr);
				
			}
		});
		return rowlayout;
	}	
	
	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isEmpty() {
		return false;
	}

	public void registerDataSetObserver(DataSetObserver arg0) {		
	}

	public void unregisterDataSetObserver(DataSetObserver arg0) {
	}

	public boolean areAllItemsEnabled() {
		return false;
	}

	public boolean isEnabled(int position) {
		return false;
	}

}
